package com.bw.controller;

import com.bw.pojo.Goods;
import com.bw.service.GoodsService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

/***************************
 FileAction:

 CreateTime:2022-08-01 13:52
 Author:Aaking-aline
 ****************************/
@Controller
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@SneakyThrows
	@RequestMapping("add")
	public String insertGoods(@RequestParam("goods_name")String a1,
							  @RequestParam("goods_price")String a2,
							  @RequestParam("goods_des")String a3,
							  @RequestParam("goods_img")MultipartFile file){

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String goodsDateTime = sdf.format(date);  // "2022-08-04 09:15:25"

		Goods goods = new Goods(null, a1, Double.parseDouble(a2), a3, goodsDateTime, goodsDateTime, null);

		goodsService.insertGoods(goods,file);

		return "redirect:message.html";
	}

	@RequestMapping("selAll")
	public String selectAllGoods(){
		goodsService.selectAllGoods();
		return "redirect:message.html";
	}


	@RequestMapping("selPage")
	@ResponseBody
	public String selectPage(String page){
		String json = goodsService.selectPageGoods(page,5);
		return json;
	}


	@RequestMapping("getPageNum")
	@ResponseBody
	public String getPageNum(){
		String pageNum = goodsService.getPageNum();
		return pageNum;
	}

	@RequestMapping("del")
	public String deleteGoods(String id){
		goodsService.deleteGoods(id);
		return "redirect:message.html";
	}

	@SneakyThrows
	@RequestMapping("upd")
	public String updateGoods(@RequestParam("id")String a0,
							  @RequestParam("goods_name")String a1,
							  @RequestParam("goods_price")String a2,
							  @RequestParam("goods_des")String a3,
							  @RequestParam("goods_img")MultipartFile file){

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String goodsDateTime = sdf.format(date);  // "2022-08-04 09:15:25"


		Goods goods = new Goods(Integer.parseInt(a0), a1, Double.parseDouble(a2), a3, goodsDateTime, goodsDateTime, null);

		goodsService.updateGoods(goods,file);
		return "redirect:message.html";
	}

	@RequestMapping("sou")
	@ResponseBody
	public String sou(@RequestParam("b")String a0, @RequestParam("e")String a1){

		String json = goodsService.selectGoodsByPrice(a0,a1);
		return json;
	}



}
